package com.lmk.qs.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
         * Druid工具类 连接对象的获取 及资源释放
         * @author 青笙
         * @email  1460961774@qq.com
         */

public class DruidUtil {
        private static DataSource dataSource;

    static {
        Properties properties = new Properties();

        try {
            properties.load(new FileReader(DruidUtil.class.getClassLoader().getResource("druid.properties").getPath()));
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


        public static DataSource getDataSource() {
            return dataSource;
        }

        public static Connection getConnection() throws SQLException {
           return dataSource.getConnection();
        }

        public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
            if (resultSet != null) {
                close(resultSet);
            }
            if (preparedStatement != null) {
                close(preparedStatement);
            }
            if (connection != null) {
                close(connection);
            }
        }

        public static void close(Connection connection, Statement statement) {
            if (statement != null) {
                close(statement);
            }
            if (connection != null) {
                close(connection);
            }
        }

        public static void close(Connection connection, Statement statement, ResultSet resultSet) {
            if (resultSet != null) {
                close(resultSet);
            }
            if (connection != null) {
                close(connection);
            }
            if (statement != null) {
                close(statement);
            }
        }

        public static void close(Statement statement) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        public static void close(Connection connection) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        public static void close(PreparedStatement preparedStatement) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        public static void close(ResultSet resultSet) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        @Test
        public  void  text(){
            JdbcTemplate jp =new JdbcTemplate(DruidUtil.getDataSource());
            String sql1="select count(id) from emp";
            Object o = jp.queryForObject(sql1, Object.class);

            System.out.println(o);

        }



}
